//
//  VersionCheck.m
//  student
//
//  Created by liuchen on 2018/11/26.
//  Copyright © 2018年 higer. All rights reserved.
//

#define AppId @"1440695924"
#import "VersionCheck.h"
#import <AFNetworking.h>

@implementation VersionCheck

+ (instancetype)shareInstance{
    
    static VersionCheck *versionCheck = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        versionCheck = [[VersionCheck alloc] init];
    });
    return versionCheck;
}

#pragma mark ==========版本检测==========
- (void)checkVersionFromAppStore{
    [[NetworkEngine sharedClient] GET:[NSString stringWithFormat:@"%@app/dic/getMsg",NETWORK_URL] data:[NSDictionary dictionaryWithObjectsAndKeys:@"1012",@"dictNo", nil] success:^(id responseObject) {//0正常 1审核
        NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic valueForKey:@"CFBundleShortVersionString"];
        if ([[responseObject objectForKey:@"data"] count]!=0) {
            if (![[[[responseObject objectForKey:@"data"] objectAtIndex:0] objectForKey:@"name"] isEqualToString:currentVersion]) {
                [self contrastWithVersion:currentVersion];
            } else {}
        }
    } failure:^(NSString *error) {
        
    }];
}
- (void)contrastWithVersion:(NSString *)localVersion{
    NSString *url = [NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@",AppId];
    NSString *codeUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [[NetworkEngine sharedClient] POST:codeUrl data:nil success:^(id responseObject) {
        if ([[responseObject objectForKey:@"resultCount"] integerValue] == 1) {
            NSString *version = [[[responseObject objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
            if(![localVersion isEqualToString:version]){
                NSString *contentMessage = [NSString stringWithFormat:@"发现新版本v%@",[NSString stringWithFormat:@"%@",version]];
                [self jumpToAppStoreWithMessage:contentMessage];
            }else{}
        }
    } failure:^(NSString *error) {
        
    }];
}

#pragma mark ==========跳转至AppStore==========
- (void)jumpToAppStoreWithMessage:(NSString *)msg{
    UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    UIViewController *parent = rootVC;
    while ((parent = rootVC.presentedViewController) != nil ) {
        rootVC = parent;
    }
    while ([rootVC isKindOfClass:[UINavigationController class]]) {
        rootVC = [(UINavigationController *)rootVC topViewController];
    }
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"版本检测" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/app/id%@",AppId]]];
    }]];
    [rootVC presentViewController:alertVC animated:YES completion:nil];
}


@end
